package com.tedu.egweibo.controller;

import com.tedu.egweibo.mapper.UserMapper;
import com.tedu.egweibo.pojo.dto.UserRegDTO;
import com.tedu.egweibo.pojo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@Slf4j
@Api(tags = "用户模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/v1/user/reg")
    @ApiOperation("用户注册")
    public int reg(@RequestBody UserRegDTO userRegDTO){
        if (userMapper.selectByUsername(userRegDTO.getUsername())==null){
            User user = new User();
            BeanUtils.copyProperties(userRegDTO,user);
            user.setCreated(new Date());
            userMapper.insert(user);
            return 1;
        }
        return 2;
    }
}
